package com.yunfeisoft.base.dao.impl;

import com.yunfeisoft.base.dao.inter.WorkflowTaskDao;
import com.yunfeisoft.base.model.User;
import com.yunfeisoft.base.model.WorkflowDefine;
import com.yunfeisoft.base.model.WorkflowInstance;
import com.yunfeisoft.base.model.WorkflowTask;
import com.yunfeisoft.common.base.BaseDaoImpl;
import com.yunfeisoft.common.sql.builder.SelectBuilder;
import com.yunfeisoft.common.sql.builder.WhereBuilder;
import com.yunfeisoft.common.utils.Page;
import org.springframework.stereotype.Repository;

import java.util.Map;

/**
 * ClassName: WorkflowTaskDaoImpl
 * Description: 流程实例执行Dao实现
 * Author: Jackie liu
 * Date: 2020-01-03
 */
@Repository
public class WorkflowTaskDaoImpl extends BaseDaoImpl<WorkflowTask, String> implements WorkflowTaskDao {

    @Override
    public Page<WorkflowTask> queryPage(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        if (params != null) {
            initPageParam(wb, params);
            wb.andEquals("t.executor", params.get("executor"));
            wb.andEquals("t.status", params.get("status"));
            wb.andFullLike("i.title", params.get("title"));
            wb.andFullLike("u.name", params.get("instanceCreateName"));
        }

        SelectBuilder builder = getSelectBuilder("t");
        builder.column("i.title as instanceTitle")
                .column("i.formId as instanceFormId")
                .column("d.formPath as defineFormPath")
                .column("u.name as instanceCreateName")
                .column("i.workflowDefineId as workflowDefineId")
                .join(WorkflowInstance.class).alias("i").on("t.workflowInstanceId = i.id").build()
                .join(WorkflowDefine.class).alias("d").on("i.workflowDefineId = d.id").build()
                .join(User.class).alias("u").on("i.createId = u.id");

        return queryPage(builder.getSql(), wb);
    }

    @Override
    public int remove(String id) {
        String sql = String.format("DELETE FROM %s WHERE ID_ = ?", domainModel.getTableName());
        return jdbcTemplate.update(sql, id);
    }
}